package com.weiter.web.controller.monitor;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiter.common.core.core.controller.BaseController;
import com.weiter.common.core.core.domain.AjaxResult;
import com.weiter.common.core.core.domain.entity.SysLogininfor;
import com.weiter.common.core.core.page.PageQuery;
import com.weiter.common.core.core.page.TableDataInfo;
import com.weiter.common.core.utils.poi.ExcelUtil;
import com.weiter.common.framework.web.service.SysPasswordService;
import com.weiter.common.log.annotation.Log;
import com.weiter.common.log.enums.BusinessType;
import com.weiter.system.service.ISysLogininforService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录日志
 * @author weiter
 */
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController {

    @Resource
    private ISysLogininforService logininforService;

    @Resource
    private SysPasswordService passwordService;

    /**
     * 查询登录日志分页列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    public TableDataInfo<SysLogininfor> list(SysLogininfor query, PageQuery pageQuery) {
        Page<SysLogininfor> page = logininforService.selectLogininforPage(query, pageQuery);
        return TableDataInfo.build(page);
    }

    /**
     * 导出登录日志
     */
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor) {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

    /**
     * 删除登录日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        return toAjax(logininforService.removeById(infoIds));
    }

    /**
     * 清空登录日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        logininforService.cleanLogininfor();
        return success();
    }

    /**
     * 账户解锁
     */
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public AjaxResult unlock(@PathVariable("userName") String userName) {
        passwordService.clearLoginRecordCache(userName);
        return success();
    }
}
